package net.lucamasini.ricette.client.util;

import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.ValueBoxBase;

public class DigitFilter {

	private final static KeyPressHandler digitOnlyHandler = new KeyPressHandler() {

		public void onKeyPress(KeyPressEvent event) {
			if (!Character.isDigit(event.getCharCode())) {
				((TextBox) event.getSource()).cancelKey();
			}
		}
	};

	public static <T> void applyTo(ValueBoxBase<T> tb) {
		tb.addKeyPressHandler(digitOnlyHandler);
	}
}
